package tld.test.interpreter
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	import tld.test.interpreter.runtime.Runtime;
	
	public class ArrayParseTest extends Sprite implements IContext
	{
		private static const _program0:String =
			<![CDATA[
			(test-array '(1 2 3 "blah" (+ 3 7)))
				]]>.toString();

		private static const _program1:String =
			<![CDATA[
			(test-two-arrays '(1 2 3 "blah" (+ 3 7))
			  '(23 "string" 34 (expt 2 3)))
				]]>.toString();

		private var _parent:IContext;
		
		private const _names:Object =
		{
			"test-array" : testArray,
			"test-two-arrays" :testTwoArrays
		}
		
		public function ArrayParseTest()
		{
			super();
			if (super.stage) this.init();
			else super.addEventListener(
				Event.ADDED_TO_STAGE, this.init);
		}
		
		private function init(event:Event = null):void
		{
			super.removeEventListener(
				Event.ADDED_TO_STAGE, this.init);
			
			var runtime:Runtime = new Runtime();
			runtime.addContext(this);
			runtime.run(_program0);
			runtime.run(_program1);
		}
		
		private function testArray(tested:Array):void
		{
			if (tested) trace("success", tested);
			else trace("failure");
		}
		
		private function testTwoArrays(arrayA:Array, arrayB:Array):void
		{
			if (arrayA && arrayB) trace("success", arrayA, arrayB);
			else trace("failure", arrayA, arrayB);
		}
		
		public function setParent(parent:IContext):IContext
		{
			if (this._parent) parent.setParent(this._parent);
			return this._parent = parent;
		}
		
		public function getParent():IContext
		{
			return this._parent;
		}
		
		public function getNames():Object
		{
			var result:Object;
			if (this._parent)
				result = Context.merge(
					this._names, this._parent.getNames());
			else result = this._names;
			return result;
		}
		
		public function put(name:String, value:Object = null):void
		{
			this._names[name] = value;
		}
		
		public function resolve(name:String):Object
		{
			return this._names[name];
		}
	}
}